package com.demo.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * @author gy
 * @date 2022/5/14
 * 利用 TransferFrom完成拷贝
 */
public class NIOFileChannel04 {
    public static void main(String[] args) throws Exception{
        // 创建相关流
        FileInputStream fileInputStream = new FileInputStream("20220514-netty-study/src/main/resources/莫兰特.png");
        FileOutputStream fileOutputStream = new FileOutputStream("20220514-netty-study/src/main/resources/莫兰特2.png");

        // 获取各个流对应的fileChannel
        FileChannel srcChannel = fileInputStream.getChannel();
        FileChannel targetChannel = fileOutputStream.getChannel();

        // 使用transferForm完成拷贝
        targetChannel.transferFrom(srcChannel,0,srcChannel.size());

        // 关闭相关的通道和流
        srcChannel.close();
        targetChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
